package com.IClass.IActivity;

import com.IClass.App.IClass;
import com.IClass.App.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class IRegisterActivity extends Activity{

	public EditText username;
	public EditText password;
	public EditText repassword;
	public EditText email;
	public Button registerbtn;
	public TextView showtext;
	public Button loginbtn;
	
	public ProgressDialog progressDialog;
	public Handler mhandle = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.iregister_layout);
		
		username = ((EditText)findViewById(R.id.username));
		password = ((EditText)findViewById(R.id.passsword));
		repassword = ((EditText)findViewById(R.id.repassword));
		email = ((EditText)findViewById(R.id.email));
		registerbtn = ((Button)findViewById(R.id.regetbtn));
		//showtext = ((TextView)findViewById(R.id.showtext));
		registerbtn = ((Button)findViewById(R.id.registerbtn));
		loginbtn = ((Button)findViewById(R.id.loginbtn));
		
		
		registerbtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// FIXME Auto-generated method stub
				  
				if(username.getText().toString().length() < 5)
				{
					showtext.setText("请输入5位以上数字或英文用户名");
					return;
				}
				if(password.getText().toString().length() < 5)
				{
					showtext.setText("输入5位以上密码");
					return;
				}
				if(repassword.getText().toString().length() < 5)
				{
					showtext.setText("输入5位以上密码");
					return;
				}
				if(!password.getText().toString().equals(repassword.getText().toString()))
				{
					showtext.setText("密码不一致");
					return;
				}
				if(email.getText().toString().length() == 0)
				{
					showtext.setText("输入email地址以便找回密码");
					return;
				}	
				
				new Thread() {
		            @Override
		            public void run() {
		            	
						mhandle.post(new Runnable() {
							@Override
							public void run() {
								progressDialog = ProgressDialog.show(
										IRegisterActivity.this, "请稍等...",
										"注册中...", true);
							}

						});
		            	
		            	
						String result = IClass.classapi.Register(username.getText().toString(),password.getText().toString(),email.getText().toString());
						if(result != null && Integer.valueOf(result) > 0)
						{
							//showtext.setText("注册中成功!");
							mhandle.post(new Runnable() {
								@Override
								public void run() {
									progressDialog.dismiss();
								}

							});
				              
							
							mhandle.post(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(IRegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
								}

							});
							
							
							IClass.setUserid(result);
							
							Editor sharedata = getSharedPreferences("IClass", MODE_PRIVATE).edit();  
							sharedata.putString("userid",result);
							sharedata.commit();
							
							
						}
						else if(result != null && Integer.valueOf(result) == -3)
						{
							mhandle.post(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(IRegisterActivity.this, "用户名或者email注册重复,请点击登录或者换登录用户名或者email!", Toast.LENGTH_SHORT).show();
								}

							});
							//showtext.setText("用户名或者email注册重复,请点击登录或者换登录用户名或者email!");
							
						}
						else
						{
							mhandle.post(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(IRegisterActivity.this, "注册中失败,请换一个email 或者登录用户名!", Toast.LENGTH_SHORT).show();
								}

							});
							//showtext.setText("注册中失败,请换一个email 或者登录用户名!");
							
						}
						
		            }
		        }.start();
		        
				
				
					
			}
			
		});
		
		loginbtn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(username.getText().toString().length() < 5)
				{
					showtext.setText("请输入5位以上数字或英文用户名");
					return;
				}
				if(password.getText().toString().length() < 5)
				{
					showtext.setText("输入5位以上密码");
					return;
				}
				
				new Thread() {
		            @Override
		            public void run() {
		            	//showtext.setText("登录中...");
		            	mhandle.post(new Runnable() {
							@Override
							public void run() {
								progressDialog = ProgressDialog.show(IRegisterActivity.this, "请稍等...","登录中...", true);
							}

						});
		            	
						String result = IClass.classapi.Login(username.getText().toString(),password.getText().toString());
						if(Integer.valueOf(result) > 0)
						{
							mhandle.post(new Runnable() {
								@Override
								public void run() {
									progressDialog.dismiss();
									Toast.makeText(IRegisterActivity.this, "登录成功!", Toast.LENGTH_LONG).show();
								}

							});
							
							
							//showtext.setText("登录成功!");
							
							IClass.setUserid(result);
							
							Editor sharedata = getSharedPreferences("IClass", MODE_PRIVATE).edit();  
							sharedata.putString("userid",result);
							sharedata.commit();
						}
						
						else
						{
							mhandle.post(new Runnable() {
								@Override
								public void run() {
									Toast.makeText(IRegisterActivity.this, "登录失败,请确认用户名和密码!", Toast.LENGTH_SHORT).show();
								}

							});
							//showtext.setText("注册中失败,请换一个email 或者登录用户名!");
							
						}
		            }
				}.start();
				
				
			}
			
		});
		
		
		
	}

}
